package com.shuaitao.newsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.shuaitao.newsystem.bean.News;
import com.shuaitao.newsystem.bean.NewsUsers;
import com.shuaitao.newsystem.service.NewsService;
import com.shuaitao.newsystem.service.NewsUsersService;
import com.shuaitao.newsystem.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/admin")
@Controller
public class AdminController {

    @Autowired
    private NewsService newsService;
    @Autowired
    private NewsUsersService newsUsersService;
    @Autowired
    NewsController newsController;
    @Autowired
    private TopicController topicController;

    @RequestMapping("/toAdmin")
    public String toAdmin(HttpSession session) {
//        newsController.getGroupNews(session);
        topicController.getAllTopic(session);
        return "newspages/admin";
    }
//    @RequestMapping("/login")
//    @ResponseBody
//    public R login(@RequestBody NewsUsers newsUsers) {
//        R r = newsUsersService.loginAdmin(newsUsers);
//        return r;
//    }

    @RequestMapping("/toEditPage/{nid}")
    public String toEditPage(@PathVariable Integer nid, HttpSession session) {
        News news = newsService.getById(nid);
        session.setAttribute("detailNews",news);
        return "newspages/news_modify";
    }

    @PostMapping("/editNews")
    public String editNews(@RequestBody News news) {
        newsService.update(news,new QueryWrapper<News>().eq("nid",news.getNid()));

        return "newspages/admin";
    }
}
